package leaf.science.ui.leftmenu
{
	import caurina.transitions.Tweener;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class LeftMenu extends LeafSciLeftMenuSkin
	{
		private var _items:Array;
		private var _selectedItem:MovieClip;
		private var _x:Number = -240;
		public var isOpen:Boolean;
		
		public function LeftMenu()
		{
			setup();
		}
		
		public function open ():void
		{
			isOpen = true;
			x = 0;
			visible = true;
			Tweener.addTween(this, {x:_x, time:0.2});
		}
		
		public function close ():void
		{
			isOpen = false;
			Tweener.addTween(this, {x:0, time:0.3, onComplete:function():void{visible = false}});
		}
		
		public function get selectedName ():String
		{
			return _selectedItem.text;
		}
		
		public function set selectedName (value:String):void
		{
			for each (var item:MovieClip in _items){
				if (item.text == value){
					setSelectedItem(item);
					break;
				}
			}
		}
		
		public function set selectedIndex (value:int):void
		{
			setSelectedItem(_items[Math.min(value, _items.length-1)]);
		}
		
		public function get selectedIndex ():int 
		{
			for (var i:uint=0; i<_items.length; i++){
				if (_items[i] == _selectedItem){
					return i;
				}
			}
			return -1;
		}
		
		private function setup ():void
		{
			visible = false;
			_items = [item_demos, item_features, item_spec];
			for each (var item:MovieClip in _items){
				item.stop();
				item.buttonMode = true;
				item.mouseChildren = false;
				setItemOut(item);
				item.addEventListener(MouseEvent.MOUSE_OVER, onItemOver);
				item.addEventListener(MouseEvent.MOUSE_OUT, onItemOut);
				item.addEventListener(MouseEvent.CLICK, onItemClick);
			}
			
			item_demos._txt.text = 'Animated demos';
			item_demos.text = 'demos';
			item_features._txt.text = 'Features';
			item_features.text = 'features';
			item_spec._txt.text = 'Specifications';
			item_spec.text = 'specifications';
		}
		
		private function onItemOver (evt:MouseEvent):void
		{
			var item:MovieClip = evt.target as MovieClip;
			if (item != _selectedItem) {
				setItemOver(item);
			}
		}
		private function onItemOut (evt:MouseEvent):void
		{
			var item:MovieClip = evt.target as MovieClip;
			if (item != _selectedItem) {
				setItemOut(item);
			}
		}
		private function onItemClick (evt:MouseEvent):void
		{
			var item:MovieClip = evt.target as MovieClip;
			if (item != _selectedItem) {
				setSelectedItem(item);
			}
		}
		
		private function setSelectedItem (selItem:MovieClip):void
		{
			
			for each (var item:MovieClip in _items){
				if (selItem == item){
					item.bg_mc.alpha = 1;
					item._txt.alpha = 1;
					item.bullet_mc.visible = true;
				} else {
					setItemOut(item);
				}
			}
			if (_selectedItem == selItem) return;
			_selectedItem = selItem;
			dispatchEvent(new Event(Event.CHANGE));
			LeafScience.instance.setAddress(selectedName);
		}
		
		private function setItemOver (item:MovieClip):void
		{
			item.bullet_mc.visible = false;
			item.bg_mc.alpha = 0.7;
			item._txt.alpha = 0.8;
		}
		
		private function setItemOut (item:MovieClip):void
		{
			item.bullet_mc.visible = false;
			item.bg_mc.alpha = 0;
			item._txt.alpha = 0.5;
		}
	}
}